////
//
/// \file ./src/vector_type_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/vector_type_node.hpp>

#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/ws_parser.hpp>
#include <inc/parser/parse_tree_node.hpp>
#include <inc/parser/immediate_node.hpp>
#include <inc/parser/atomic_type_node.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::scoped_ptr;

/// \brief Basic constructor for vector types
/// \author Chandler Carruth
/// \date 2008.10.04

VectorTypeNode::VectorTypeNode()
    : ws(new WSParser)
{}

/// \brief Empty destructor, all members are smart pointers
/// \author Chandler Carruth
/// \date 2008.10.04

VectorTypeNode::~VectorTypeNode()
{}

/// \brief Parse a vector type in INC
/// \author Chandler Carruth
/// \date 2008.10.04
///
/// Parse out a vector type in INC. These are started by a 'vector' keyword and
/// require both a size and element type parameter.
///
/// Example: 'vector< 4 x int >'.

bool
VectorTypeNode::parse( const iterator &first,
                       const iterator &last,
                       iterator &last_parsed )
{
  assert( get_text().empty() && !size && !element_type
          && "This node was parsed more than once!" );

  if( std::distance( first, last ) >= 7
      && string( first, first + 7 ) == "vector<" )
  {
    iterator current = first + 7;
    if( ws->parse_ws( current, last, last_parsed ) )
    {
      current = last_parsed;

      scoped_ptr<ImmediateNode> tmp_size( new ImmediateNode );
      if( tmp_size->parse( current, last, last_parsed ) )
      {
        current = last_parsed;
        if( ws->parse_ws( current, last, last_parsed ) )
        {
          current = last_parsed;
          if( current != last && *current == 'x' )
          {
            ++current;
            if( ws->parse_ws( current, last, last_parsed ) )
            {
              current = last_parsed;

              scoped_ptr<AtomicTypeNode> tmp_el_type( new AtomicTypeNode );
              if( tmp_el_type->parse( current, last, last_parsed ) )
              {
                current = last_parsed;
                if( ws->parse_ws( current, last, last_parsed ) )
                {
                  current = last_parsed;
                  if( current != last && *current == '>' )
                  {
                    last_parsed = current + 1;
                    set_text( string( first, last_parsed ) );
                    size.swap( tmp_size );
                    element_type.swap( tmp_el_type );
                    return true;
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief Convenience getter for the vector size
/// \author Chandler Carruth
/// \date 2008.10.04

ImmediateNode* VectorTypeNode::get_size() const
{
  return size.get();
}

/// \brief Convenience getter for the vector's element type
/// \author Chandler Carruth
/// \date 2008.10.04

AtomicTypeNode* VectorTypeNode::get_element_type() const
{
  return element_type.get();
}

} //  end parser namespace
} //  end inc namespace
